﻿using System;
using System.Collections;
using System.Text;
using System.Data;
using GeneralLibs;

namespace Lists
{
    public class BenhNhan : DoiTuong
    {
        #region string Ma property
        public string Ma
        {
            get
            {
                if (_DataRow["Ma"] is string) return (string)_DataRow["Ma"];
                return "";
            }
            set
            {
                _DataRow["Ma"] = value;
            }
        }
        #endregion

        #region string Ten property
        public string Ten
        {
            get
            {
                if (_DataRow["Ten"] is string) return (string)_DataRow["Ten"];
                return "";
            }
            set
            {
                _DataRow["Ten"] = value;
            }
        }
        #endregion

        #region string GioiTinh property
        public string GioiTinh
        {
            get
            {
                if (_DataRow["GioiTinh"] is string) return (string)_DataRow["GioiTinh"];
                return "";
            }
            set
            {
                _DataRow["GioiTinh"] = value;
            }
        }
        #endregion

        #region string DanToc property
        public string DanToc
        {
            get
            {
                if (_DataRow["DanToc"] is string) return (string)_DataRow["DanToc"];
                return "";
            }
            set
            {
                _DataRow["DanToc"] = value;
            }
        }
        #endregion

        #region NullableDate NgaySinh property
        public NullableDate NgaySinh
        {
            get
            {
                if (DataRow["NgaySinh"] is DateTime)
                    return new NullableDate((DateTime)DataRow["NgaySinh"]);
                else
                    return new NullableDate();
            }
            set
            {
                DataRow["NgaySinh"] = value.Date;
            }
        }
        #endregion

        #region int Tuoi property
        public int Tuoi
        {
            get
            {
                if (_DataRow["Tuoi"] is int) return (int)_DataRow["Tuoi"];
                return 0;
            }
            set
            {
                _DataRow["Tuoi"] = value;
            }
        }
        #endregion

        #region string DienThoai property
        public string DienThoai
        {
            get
            {
                if (_DataRow["DienThoai"] is string) return (string)_DataRow["DienThoai"];
                return "";
            }
            set
            {
                _DataRow["DienThoai"] = value;
            }
        }
        #endregion

        #region string Email property
        public string Email
        {
            get
            {
                if (_DataRow["Email"] is string) return (string)_DataRow["Email"];
                return "";
            }
            set
            {
                _DataRow["Email"] = value;
            }
        }
        #endregion

        #region string NgheNghiep property
        public string NgheNghiep
        {
            get
            {
                if (_DataRow["NgheNghiep"] is string) return (string)_DataRow["NgheNghiep"];
                return "";
            }
            set
            {
                _DataRow["NgheNghiep"] = value;
            }
        }
        #endregion

        #region string NoiLamViec property
        public string NoiLamViec
        {
            get
            {
                if (_DataRow["NoiLamViec"] is string) return (string)_DataRow["NoiLamViec"];
                return "";
            }
            set
            {
                _DataRow["NoiLamViec"] = value;
            }
        }
        #endregion

        #region string NguoiThan property
        public string NguoiThan
        {
            get
            {
                if (_DataRow["NguoiThan"] is string) return (string)_DataRow["NguoiThan"];
                return "";
            }
            set
            {
                _DataRow["NguoiThan"] = value;
            }
        }
        #endregion

        #region string DiaChi property
        public string DiaChi
        {
            get
            {
                if (_DataRow["DiaChi"] is string) return (string)_DataRow["DiaChi"];
                return "";
            }
            set
            {
                _DataRow["DiaChi"] = value;
            }
        }
        #endregion

        #region string GhiChu property
        public string GhiChu
        {
            get
            {
                if (_DataRow["GhiChu"] is string) return (string)_DataRow["GhiChu"];
                return "";
            }
            set
            {
                _DataRow["GhiChu"] = value;
            }
        }
        #endregion

        #region Guid CoQuanID property
        public Guid CoQuanID
        {
            get
            {
                if (_DataRow["CoQuanID"] is Guid) return (Guid)_DataRow["CoQuanID"];
                return Guid.Empty;
            }
            set
            {
                _DataRow["CoQuanID"] = value;
            }
        }
        #endregion

        #region string SoCMND property
        public string SoCMND
        {
            get
            {
                if (_DataRow["SoCMND"] is string) return (string)_DataRow["SoCMND"];
                return "";
            }
            set
            {
                _DataRow["SoCMND"] = value;
            }
        }
        #endregion

        public BenhNhan(DataRow dataRow)
            : base(dataRow)
        {
        }
    }

    public class DanhMucBenhNhan : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucBenhNhan _Instance;
        public static DanhMucBenhNhan Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucBenhNhan();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                    try
                    {
                        UniqueConstraint uniqueConstraint = new UniqueConstraint(_Instance.DataTable.Columns["Ma"]);
                        _Instance.DataTable.Constraints.Add(uniqueConstraint);
                        _Instance.DataView.Sort = "Ma DESC";
                    }
                    catch (Exception ex)
                    {
                    }
                }
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "Clinic_BenhNhan";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new BenhNhan((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public BenhNhan this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new BenhNhan(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public BenhNhan NewDoiTuong()
        {
            try
            {
                return new BenhNhan(Instance.DataTable.NewRow());
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public BenhNhan GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new BenhNhan(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public BenhNhan GetByID(Guid objID)
        {
            try
            {
                return GetByProperty("ID", objID);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public BenhNhan GetByMa(string ma)
        {
            try
            {
                DataRow[] rows = DataTable.Select(string.Format("Ma = '{0}'", ma));
                if (rows.Length > 0)
                    return new BenhNhan(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static DanhMucBenhNhan GetAll()
        {
            try
            {
                return Instance;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public DanhMucBenhNhan Filter(string filterExpression)
        {
            try
            {
                DanhMucBenhNhan obj = new DanhMucBenhNhan();
                obj.DataView = new DataView(DanhMucBenhNhan.Instance.DataTable, filterExpression, "Ma DESC", DataViewRowState.CurrentRows);
                return obj;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
    }
}
